#import "SuperViewController.h"
#import "BaseNavVC.h"
#import "KFLoginVC.h"
@interface SuperViewController ()

@property(nonatomic,retain)UIView *bottomLine;
@property(nonatomic,retain)UIButton * backBtn;
@property(nonatomic,retain)UIImageView * bgImageView,*titleImgView;
@property(nonatomic,retain)UIView * shelterView ,* dlStatueBarView;
@end

@implementation SuperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(turnToLoginView) name:ExitLoginByTokenIsExperied object:nil];
    [self createNavBar];
}
-(void)turnToLoginView{
    [Mytools deleteAllUserDefaules];
    dispatch_async(dispatch_get_main_queue(), ^{
        KFLoginVC * vc = [[KFLoginVC alloc]init];
        BaseNavVC * nav = [[BaseNavVC alloc]initWithRootViewController:vc];
        [UIApplication sharedApplication].delegate.window.rootViewController = nav;
    });
}
-(void)createNavBar{
    if (_navView == nil) {
        _navView = [[UIView alloc]initWithFrame:Frame(0, 0, SCREEN_WIDTH, self.navBarHeight==0?kTopHeight:self.navBarHeight)];
        _navView.backgroundColor=self.navBgColor==nil?[UIColor whiteColor]:self.navBgColor;
        [self.view addSubview:_navView];
        
        //这里先注释，现在暂时没有导航栏图片的需求，等到后期用懒加载实现
//        self.bgImageView = [UIImageView new];
//        [_navView addSubview:self.bgImageView];
//        self.bgImageView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
        self.titleLab = [[UILabel alloc]init];
        _titleLab.text = self.myTitle;
        _titleLab.lineBreakMode = NSLineBreakByTruncatingTail;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.font = [UIFont systemFontOfSize:AUTO(17)];
        [_navView addSubview:self.titleLab];
        _titleLab.sd_layout.centerXEqualToView(_navView).topSpaceToView(_navView, kStatusBarHeight).heightIs(kNavBarHeight);
        [_titleLab setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH *0.75];
        if (self.navigationController.viewControllers.count > 1) {
            self.backBtn = [[UIButton alloc]initWithFrame:Frame(0, kStatusBarHeight, kNavBarHeight, kNavBarHeight)];
            [_backBtn setImage:Image(@"返回") forState:UIControlStateNormal];
            [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
            [_navView addSubview:self.backBtn];
        }
        //line
        self.bottomLine=[UIView new];
        _bottomLine.backgroundColor = [UIColor colorWithHue:0.04 saturation:0.02 brightness:0.95 alpha:1];
        [_navView addSubview:_bottomLine];
        _bottomLine.sd_layout.widthIs(SCREEN_WIDTH).heightIs(0.6).bottomEqualToView(_navView);
    }
}
- (void)setStatue_Color:(UIColor *)statue_Color{
    _statue_Color = statue_Color;
    if (_dlStatueBarView == nil) {
        if (_navView == nil) {
            [self createNavBar];
        }
        _dlStatueBarView = [[UIView alloc]initWithFrame:Frame(0, 0, SCREEN_WIDTH, kStatusBarHeight)];
        [_navView addSubview:self.dlStatueBarView];
    }
    self.dlStatueBarView.backgroundColor = statue_Color;
}
- (void)setLeftBtnName:(NSString *)leftBtnName{
    _leftBtnName = leftBtnName;
    if (leftBtnName.length > 0) {
        if (_backBtn == nil) {
            self.backBtn = [[UIButton alloc]initWithFrame:Frame(10, kStatusBarHeight, kNavBarHeight, kNavBarHeight)];
            [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
            [_navView addSubview:self.backBtn];
        }
        [_backBtn setImage:nil forState:UIControlStateNormal];
        [_backBtn setTitle:leftBtnName forState:UIControlStateNormal];
        _backBtn.titleLabel.font = UIFontMake(AUTO(15));
        [_backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}
- (void)setIsHideStatueBar:(BOOL)isHideStatueBar{
    _isHideStatueBar = isHideStatueBar;
    if (_dlStatueBarView == nil) {
        if (_navView == nil) {
            [self createNavBar];
        }
        _dlStatueBarView = [[UIView alloc]initWithFrame:Frame(0, 0, SCREEN_WIDTH, kStatusBarHeight)];
        [_navView addSubview:self.dlStatueBarView];
    }
    self.dlStatueBarView.hidden = isHideStatueBar;
}
-(void)setRightBtnName:(NSString *)rightBtnName{
    if (_rightBtn ==nil) {
        _rightBtnName = rightBtnName;
        [self createRightButton];
    }else{
        [_rightBtn setTitle:rightBtnName forState:UIControlStateNormal];
    }
}
-(void)setRightBtnImage:(UIImage *)rightBtnImage{
    if (rightBtnImage != nil) {
        _rightBtnImage = rightBtnImage;
    }else{
        _rightBtnImage = [UIImage imageNamed:@""];
    }
    if (_rightBtn ==nil) {
        [self createRightButton];
        //如果是图片就改变约束
        _rightBtn.mm_width(kNavBarHeight).mm_height(kNavBarHeight).mm_right(10).mm_top(kStatusBarHeight);
    }else{
        [_rightBtn setImage:rightBtnImage forState:UIControlStateNormal];
    }
}
-(void)createRightButton{
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    if (_rightBtnImage!=nil) {
        [_rightBtn setImage:_rightBtnImage forState:UIControlStateNormal];
    }
    if (_rightBtnName.length>0) {
        [_rightBtn setTitle:_rightBtnName forState:UIControlStateNormal];
    }
    [_navView addSubview:self.rightBtn];
    CGFloat wid = [_rightBtnName sizeWithFont:UIFontMake(_rightBtnFontSize) maxSize:CGSizeMake(300, 300)].width;
    _rightBtn.mm_width(wid+10<kNavBarHeight?kNavBarHeight:wid+10).mm_height(kNavBarHeight).mm_top(kStatusBarHeight).mm_right(10);
}
-(void)setNavViewImg:(UIImage *)navViewImg{
    if (navViewImg!=nil) {
        _navViewImg = navViewImg;
        self.bgImageView.image = _navViewImg;
    }else{
        if (_bgImageView==nil) {
            [self createBgImageView];
        }else{
            _bgImageView.image = navViewImg;
        }
    }
}
-(void)createBgImageView{
    
    self.bgImageView = [UIImageView new];
    if (_navViewImg!=nil) {
        self.bgImageView.image = _navViewImg;
    }
    [_navView sendSubviewToBack:self.bgImageView];
    _bgImageView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
}
-(void)setRightBtnNameColor:(UIColor *)rightBtnNameColor{
    if (rightBtnNameColor !=nil) {
        _rightBtnNameColor = rightBtnNameColor;
    }else{
        _rightBtnNameColor = [UIColor whiteColor];
    }
    [self.rightBtn setTitleColor:self.rightBtnNameColor forState:0];
}
-(void)setIsHideBottomLine:(BOOL)isHideBottomLine{
    if(isHideBottomLine==YES){
        self.bottomLine.hidden = YES;
    }else{
        self.bottomLine.hidden = NO;
    }
}
-(void)setRightBtnFontSize:(CGFloat)rightBtnFontSize{
    if (rightBtnFontSize>0) {
        _rightBtnFontSize = rightBtnFontSize;
    }else{
        _rightBtnFontSize = 17;
    }
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:self.rightBtnFontSize];
    CGFloat wid = [_rightBtnName sizeWithFont:UIFontMake(_rightBtnFontSize) maxSize:CGSizeMake(300, 300)].width;
    _rightBtn.mm_width(wid+10<kNavBarHeight?kNavBarHeight:wid+10).mm_height(kNavBarHeight).mm_top(kStatusBarHeight).mm_right(10);
}
-(void)setNavBarHeight:(CGFloat)navBarHeight{
    if (navBarHeight>0) {
        _navBarHeight = navBarHeight;
    }else{
        _navBarHeight = kTopHeight;
    }
    _navView.height = _navBarHeight;
}
-(void)setIsHideNavBar:(BOOL)isHideNavBar{
    _isHideNavBar = isHideNavBar;
    _navView.hidden = isHideNavBar;
}
-(void)setIsHideLeftBtn:(BOOL)isHideLeftBtn{
    if (isHideLeftBtn == YES) {
        [self.backBtn removeFromSuperview];
        self.backBtn = nil;
    }
}
-(void)setMyTitle:(NSString *)myTitle{
    if (myTitle.length>0) {
        _myTitle = myTitle;
    }else{
        _myTitle = @"";
    }
    _titleLab.text = self.myTitle;
}
- (void)setTitleImg:(UIImage *)titleImg{
    if (titleImg) {
        [self addTitleImgViewToNav];
        _titleImgView.image = titleImg;
    }
}
-(void)addTitleImgViewToNav{
    [_navView addSubview:self.titleImgView];
    if (self.titleLab.text.length) {
        _titleImgView.sd_layout.rightSpaceToView(_titleLab, 5).centerYEqualToView(_titleLab).offset(kStatusBarHeight/2.0).heightIs(30).widthEqualToHeight();
    }else{
        _titleImgView.sd_layout.centerYEqualToView(_navView).offset(kStatusBarHeight/2.0).centerXEqualToView(_navView).widthEqualToHeight().heightIs(30);
    }
}
- (void)setTitleImgUrl:(NSString *)titleImgUrl{
    if (titleImgUrl.length) {
        [self addTitleImgViewToNav];
        [_titleImgView sd_setImageWithURL:URL(titleImgUrl)];
    }
}
- (UIImageView *)titleImgView{
    if (_titleImgView==nil) {
        _titleImgView = [UIImageView new];
    }
    return _titleImgView;
}
- (void)setTitleImgCorner:(CGFloat)titleImgCorner{
    _titleImgCorner = titleImgCorner;
    if (self.titleImgView) {
        self.titleImgView.cornerRadius = titleImgCorner;
    }
}
- (void)setTitleImgSize:(CGSize)titleImgSize{
    _titleImgSize = titleImgSize;
    if (self.titleImgView) {
        _titleImgView.sd_layout.widthIs(titleImgSize.width).heightIs(titleImgSize.height);
    }
}
-(void)setLeftBtnImage:(NSString *)leftBtnImage{
    if (leftBtnImage) {
        if (_backBtn == nil) {
            self.backBtn = [[UIButton alloc]initWithFrame:Frame(10, kStatusBarHeight, kNavBarHeight, kNavBarHeight)];
            [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
            [_navView addSubview:self.backBtn];
        }
        [_backBtn setImage:Image(leftBtnImage) forState:UIControlStateNormal];
    }
}
-(void)setNavTitleFontSize:(CGFloat)navTitleFontSize{
    if (navTitleFontSize>0) {
        _navTitleFontSize = navTitleFontSize;
    }else{
        _navTitleFontSize = 17;
    }
    _titleLab.font = [UIFont systemFontOfSize:self.navTitleFontSize];
}
-(void)setNavBgColor:(UIColor *)navBgColor{
    if (navBgColor !=nil) {
        _navBgColor = navBgColor;
        if ([self isTheSameColor2:navBgColor anotherColor:[UIColor whiteColor]]) {//如果用户设置了导航条背景色那就判断下
            _titleLab.textColor = [UIColor blackColor];
        }else{
            _titleLab.textColor = [UIColor whiteColor];
        }
    }else{
        _navBgColor = ThemeColor;//这里自己用
    }
    self.navView.backgroundColor = _navBgColor;
    [self setNeedsStatusBarAppearanceUpdate];//刷新状态栏重新调用preferredStatusBarStyle
}
-(void)setMyTitleColor:(UIColor *)myTitleColor{
    if (myTitleColor!=nil) {
        _titleLab.textColor = myTitleColor;
    }else{
        _titleLab.textColor = [UIColor blackColor];
    }
}
- (void)setNavTitleFont:(UIFont *)navTitleFont{
    _navTitleFont = navTitleFont;
    _titleLab.font = navTitleFont;
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    if (self.navView !=nil) {
        BOOL isWhite = [self isTheSameColor2:self.navView.backgroundColor anotherColor:[UIColor whiteColor]];
        if (isWhite) {
            return UIStatusBarStyleDefault;
        }else{
            return  UIStatusBarStyleLightContent;
        }
    }else{
        return UIStatusBarStyleDefault;
    }
}

- (BOOL) isTheSameColor2:(UIColor*)color1 anotherColor:(UIColor*)color2{
    return  CGColorEqualToColor(color1.CGColor, color2.CGColor);
}
#pragma mark 点击事件
-(void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightBtnClick{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end

